import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 常量路由 - 不需要权限验证的路由
export const constantRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '操作台', icon: 'dashboard', roles: ['admin','teacher','student'] }
    }]
  },
  
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  
  {
    path: '/register',
    component: () => import('@/views/register/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  }
]

// 异步路由 - 需要根据用户角色动态加载的路由
export const asyncRoutes = [
  // 管理员路由
  {
    path: '/user',
    component: Layout,
    children: [{
      path: 'index',
      name: 'UserManagement',
      component: () => import('@/views/user/index'),
      meta: { title: '用户管理', icon: 'el-icon-s-check', roles: ['admin'] }
    }]
  },

  // 学术活动模块
  {
    path: '/activity',
    component: Layout,
    redirect: '/activity/list',
    name: 'Activity',
    meta: { title: '学术活动', icon: 'table' },
    children: [
      {
        path: 'list',
        name: 'ActivityList',
        component: () => import('@/views/activity/list/index'),
        meta: { title: '活动列表', icon: 'table', roles: ['student'] }
      },
      {
        path: 'my-activities',
        name: 'MyActivities',
        component: () => import('@/views/activity/my-activities/index'),
        meta: { title: '我的活动', icon: 'user', roles: ['student'] }
      },
      {
        path: 'manage',
        name: 'ActivityManage',
        component: () => import('@/views/activity/manage/index'),
        meta: { title: '活动管理', icon: 'form', roles: ['teacher', 'admin'] }
      },
      {
        path: 'publish',
        name: 'ActivityPublish',
        component: () => import('@/views/activity/publish/index'),
        meta: { title: '发布活动', icon: 'form', roles: ['admin','teacher'] },
        hidden: true
      },
      {
        path: 'detail/:id',
        name: 'ActivityDetail',
        component: () => import('@/views/activity/detail/index'),
        meta: { title: '活动详情', roles: ['teacher','admin','student'] },
        hidden: true
      }
    ]
  },

  // 成果申报模块
  {
    path: '/achievement',
    component: Layout,
    redirect: '/achievement/my-achievements',
    name: 'Achievement',
    meta: { title: '成果申报', icon: 'el-icon-s-promotion' },
    children: [
      {
        path: 'my-achievements',
        name: 'MyAchievements',
        component: () => import('@/views/achievement/my-achievements/index'),
        meta: { title: '我的成果', icon: 'el-icon-tickets', roles: ['student'] }
      },
      {
        path: 'apply',
        name: 'AchievementApply',
        component: () => import('@/views/achievement/apply/index'),
        meta: { title: '申报成果', icon: 'edit', roles: ['student'] },
        hidden: true
      },
      {
        path: 'teacher-review',
        name: 'TeacherReview',
        component: () => import('@/views/achievement/teacher-review/index'),
        meta: { title: '教师审核', icon: 'el-icon-coordinate', roles: ['teacher'] }
      },
      {
        path: 'admin-approve',
        name: 'AdminApprove',
        component: () => import('@/views/achievement/admin-approve/index'),
        meta: { title: '管理员审批', icon: 'example', roles: ['admin'] }
      },
      {
        path: 'detail/:id',
        name: 'AchievementDetail',
        component: () => import('@/views/achievement/detail/index'),
        meta: { title: '成果详情', roles: ['student', 'teacher', 'admin'] },
        hidden: true
      }
    ]
  },
  
  // 科研项目管理模块
  {
    path: '/research',
    component: Layout,
    redirect: '/research/projects',
    name: 'Research',
    meta: { title: '科研项目', icon: 'el-icon-s-cooperation', roles: ['teacher','student'] },
    children: [
      {
        path: 'projects',
        name: 'ResearchProjects',
        component: () => import('@/views/research/projects/index'),
        meta: { title: '我的项目', icon: 'el-icon-s-management', roles: ['teacher'] }
      },
      {
        path: 'create',
        name: 'CreateProject',
        component: () => import('@/views/research/create/index'),
        meta: { title: '创建项目', icon: 'el-icon-plus', roles: ['teacher'] },
        hidden: true
      },
      {
        path: 'detail/:id',
        name: 'ProjectDetail',
        component: () => import('@/views/research/detail/index'),
        meta: { title: '项目详情', roles: ['teacher', 'student'] },
        hidden: true
      },
      {
        path: 'milestones/:id',
        name: 'ProjectMilestones',
        component: () => import('@/views/research/milestones/index'),
        meta: { title: '项目节点', roles: ['teacher'] },
        hidden: true
      },
      {
        path: 'student-projects',
        name: 'StudentProjects',
        component: () => import('@/views/research/student-projects/index'),
        meta: { title: '我参与的项目', icon: 'el-icon-s-order', roles: ['student'] }
      },
      {
        path: 'invitations',
        name: 'ProjectInvitations',
        component: () => import('@/views/research/invitations/index'),
        meta: { title: '项目邀请', icon: 'el-icon-message', roles: ['student'] }
      },
      {
        path: 'student-detail/:id',
        name: 'StudentProjectDetail',
        component: () => import('@/views/research/student-detail/index'),
        meta: { title: '项目详情', roles: ['student'] },
        hidden: true
      }
    ]
  },

  {
    path: '/profile',
    component: Layout,
    children: [{
      path: 'index',
      name: 'Profile',
      component: () => import('@/views/profile/index'),
      meta: { title: '个人中心', icon: 'user', roles: ['admin','teacher','student'] }
    }]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router